#include "socketclient.h"

class Command;

SocketClient::SocketClient(qintptr socketdescriptor,QLocalSocket *socket)
{
    m_socket = socket;
    m_socketdescriptor = socketdescriptor;

    connect(m_socket, SIGNAL(readyRead()), this, SLOT(OnReadData()));
    connect(m_socket, SIGNAL(disconnected()), this, SLOT(OnDisconnect()));
}

SocketClient::~SocketClient(){
    m_socket->close();
}

void SocketClient::OnDisconnect(){
    qDebug()<<"client disconnect";
    emit Disconnect(m_socketdescriptor);
}

void SocketClient::OnReadData(){
    QByteArray data = m_socket->readAll();
    qDebug()<<QString(data);
    try {
        Command command(data);
        emit BackGroundCommand(command);
    } catch (QString e) {
         qDebug() << e;
         return;
    }
}
